//
//  BarLayerView.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/25.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//
#import "BarLayerView.h"
#import "ZCBar.h"
#define BTN_BASE_TAG 2034
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@implementation BarLayerView


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.barArr = [[NSMutableArray alloc]initWithCapacity:0];
        
        
    }
    return self;
}
-(void)setXArr:(NSMutableArray *)xArr
{
    _xArr = xArr;
}
-(void)setDataArr:(NSMutableArray *)dataArr
{
    _dataArr = dataArr;
}

-(void)strokeMKBarChart
{
    //删除所有的柱子
    [self viewCleanupForCollection:self.barArr];
    [self upDateMKBarChart];
}
//绘图
-(void)upDateMKBarChart
{
    
//    CGFloat barInterval = (self.bounds.size.width-(25*(self.xArr.count+1)))/(self.xArr.count-1);
    
    for (int a = 0; a < self.xArr.count; a++)
    {
        
        ZCBar *bar = [[ZCBar alloc]initWithFrame:CGRectMake(10+(CHART_BAR_WIDTH + CHART_BAR_INTER)*a, 0, CHART_BAR_WIDTH+5.0f, self.bounds.size.height)];
        bar.backgroundColor = [UIColor clearColor];
        [bar setDataArr:self.dataArr[a]];
        [bar setTitleStr:self.xArr[a]];
        bar.tag = BTN_BASE_TAG + a;
        [bar addTarget:self action:@selector(barAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.barArr addObject:bar];//添加进数组中,重绘时删除,重新创建
        
        [self addSubview:bar];
    }
}
-(void)barAction:(ZCBar *)bar{
    if (self.delegate && [self.delegate respondsToSelector:@selector(barLayerView:didSelectedOfIndex:)]) {
        [self.delegate barLayerView:self didSelectedOfIndex:bar.tag - BTN_BASE_TAG ];
    }
}
//删除所有的柱子
- (void)viewCleanupForCollection:(NSMutableArray *)array
{
    if (array.count) {
        [array makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [array removeAllObjects];
    }
}

@end
